/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method;


import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.entity.entity.ICefData;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.AccessorClassGenContext;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.AccessorClassTypeInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property.NestedAccessorFieldGenerator;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CastExpression;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.InfixExpression;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

//
//        public override ICefData copySelf()
//		{
//			var data = (ICefEver)base.CopySelf();
//			
//			data.TestDecimal = TestDecimalAccessorField?.CopySelf();
//			
//			return data;
//		}
//     
public class AccessorCopySelfGen extends ClassMethodGenerator
{

	private AccessorClassGenContext parentContext;
	private MediateType entityInterfaceType;
	private IGspCommonDataType dataType;
	private AccessorClassTypeInfo accessorClassTypeInfo;
	public AccessorCopySelfGen(AccessorClassGenContext parentContext,MediateType entityInterfaceType, IGspCommonDataType dataType, AccessorClassTypeInfo accessorClassTypeInfo)
	{
		this.parentContext = parentContext;
		this.entityInterfaceType = entityInterfaceType;
		this.dataType = dataType;
		this.accessorClassTypeInfo = accessorClassTypeInfo;
	}

	@Override
	protected Block buildMethodBody() {
		Block block=ast.newBlock();
		addDeclareDataStatement(block);
		addPropertyStatements(block);
		addReturnStatement(block);
		return block;
	}

	private void addReturnStatement(Block block) {
		ReturnStatement returnStatement=ast.newReturnStatement();
		returnStatement.setExpression(ast.newSimpleName("data"));
		block.statements().add(returnStatement);
	}

	private void addPropertyStatements(Block block) {
		for (IGspCommonField field:dataType.getContainElements()) {
			if (field.getObjectType()== GspElementObjectType.DynamicProp) {
				addNestedPropertyStatements(field, block);
			} else if(field.getIsUdt()){
				addNestedPropertyStatements(field, block);
		}
		}
	}

	private void addNestedPropertyStatements(IGspCommonField field, Block block)
	{
		IfStatement ifStatement=ast.newIfStatement();
		addNestedIfStatementExpression(ifStatement,field);
		addNestedIfStatementThen(ifStatement,field);
		block.statements().add(ifStatement);
	}

	private void addNestedIfStatementExpression(IfStatement ifStatement, IGspCommonField field) {

		InfixExpression infixExpression=ast.newInfixExpression();
		infixExpression.setOperator(InfixExpression.Operator.NOT_EQUALS);
		infixExpression.setLeftOperand(ast.newSimpleName(NestedAccessorFieldGenerator.getFieldName(field)));
		infixExpression.setRightOperand(ast.newNullLiteral());
		ifStatement.setExpression(infixExpression);
	}

	private void addNestedIfStatementThen(IfStatement ifStatement, IGspCommonField field) {
		Block block=ast.newBlock();
		MethodInvocation methodInvocationSetProp=ast.newMethodInvocation();
		methodInvocationSetProp.setName(ast.newSimpleName("set"+field.getLabelID()));

//		Assignment assignment=ast.newAssignment();
//
//		assignment.setLeftHandSide(ast.newQualifiedName(ast.newSimpleName("data"),ast.newSimpleName(NestedAccessorFieldGenerator.getFieldName(propertyGetAtrribute.propName()))));
//		assignment.setOperator(Assignment.Operator.ASSIGN);

		CastExpression castExpression=ast.newCastExpression();
		castExpression.setType(parentContext.getEntityDataGenContext().getFieldTypeInfo(dataType.getCode(),field).getType(ast));

		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setExpression(ast.newSimpleName(NestedAccessorFieldGenerator.getFieldName(field)));
		methodInvocation.setName(ast.newSimpleName("copySelf"));
		castExpression.setExpression(methodInvocation);
		methodInvocationSetProp.arguments().add(castExpression);
		methodInvocationSetProp.setExpression(ast.newSimpleName("data"));
//		assignment.setRightHandSide(castExpression);
//		ExpressionStatement expressionStatement=ast.newExpressionStatement(assignment);
//		block.statements().add(expressionStatement);
		block.statements().add(ast.newExpressionStatement(methodInvocationSetProp));
		ifStatement.setThenStatement(block);
	}

	private void addDeclareDataStatement(Block block) {
		VariableDeclarationFragment variableDeclarationFragment=ast.newVariableDeclarationFragment();

		variableDeclarationFragment.setName(ast.newSimpleName("data"));

		CastExpression castExpression=ast.newCastExpression();
		castExpression.setType(ast.newSimpleType(ast.newName(entityInterfaceType.getFullName())));

		SuperMethodInvocation superMethodInvocation=ast.newSuperMethodInvocation();
		superMethodInvocation.setName(ast.newSimpleName("innerCopySelf"));
		castExpression.setExpression(superMethodInvocation);
		variableDeclarationFragment.setInitializer(castExpression);

		VariableDeclarationStatement variableDeclarationStatement =ast.newVariableDeclarationStatement(variableDeclarationFragment);
		variableDeclarationStatement.setType(ast.newSimpleType(ast.newName(entityInterfaceType.getFullName())));
		block.statements().add(variableDeclarationStatement);

	}

	public static final String MethodName = "innerCopySelf";

	@Override
	protected String getMethodName(){return MethodName;}

	@Override
	protected boolean getIsOverride() {
		return true;
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
		ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
		list.add(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
		return list;
	}

	@Override
	protected TypeInfo getReturnType() {
		return new TypeInfo(ICefData.class.getTypeName());
	}

	@Override
	protected ArrayList<ParameterInfo> getParameterCollection() {
		return null;
	}
}
